﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NHibernate;

namespace Persistence
{
    /// <summary>
    /// HttpModule to handle nHibernate session throughout HTTP requests
    /// </summary>
    public class NHibernateHttpModule : IHttpModule
    {
        /// <summary>
        /// Constant for storing value of nHibernate session id in HTTP Context
        /// </summary>
        public const string Key_Session = "nHibernateSession";

        /// <summary>
        /// Default constructor
        /// </summary>
        public NHibernateHttpModule()
        {
        }

        #region IHttpModule Members

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
        }

        #endregion

        #region Session handling

        private void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            SetSessionInContext(context, Key_Session, NHibernateSessionHelper.GetSession());
        }

        private void context_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;

            ISession session = GetSessionFromContext(context, Key_Session);
            if (session != null)
            {
                try
                {
                    session.Flush();
                    session.Close();
                }
                catch { }
            }
            SetSessionInContext(context, Key_Session, null);
        }

        /// <summary>
        /// Sets current nHibernate Session in HTTP Context created by each HTTP request.
        /// </summary>
        public static void CurrentSession()
        {
            try
            {
                HttpContext currentContext = HttpContext.Current;
                ISession session = null;
                if (currentContext != null)
                {
                    session = GetSessionFromContext(currentContext, Key_Session);
                    if (session == null)
                    {
                        session = NHibernateSessionHelper.GetSession();
                        SetSessionInContext(currentContext, Key_Session, session);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        #region > Helpers
        
        /// <summary>
        /// Saves current session into (current) HTTP Context object.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="Key_Session"></param>
        /// <param name="nHibernateSession"></param>
        private static void SetSessionInContext(HttpContext context, string Key_Session, ISession nHibernateSession)
        {
            context.Items[Key_Session] = nHibernateSession;
        }

        /// <summary>
        /// Retrieves current session from (current) HTTP Context object.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="Key_Session"></param>
        private static ISession GetSessionFromContext(HttpContext context, string Key_Session)
        {
            return (ISession)context.Items[Key_Session];
        }
        
        #endregion

        #endregion
    }
}
